/******************************************************
 * Graduate Project
 * @author Hui Liu
 * @dueDate May. 14st, 2011
 * @purpose
 * Define database
 * Create database when it is not exist
 * Update database when it is out of version
 * @algorithm
 *  See map class
 * 
 * @input
 * 
 * @output
 *
*********************************************************/
package org.TUApp.eTour;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// TODO: Auto-generated Javadoc
// Helper class for opening, creating, and managing database version control
/**
 * The Class TUeTourDBHelper.
 */
public class TUeTourDBHelper extends SQLiteOpenHelper {

	  /** The Constant TAG. */
  	private static final String TAG = TUeTourDBHelper.class.getSimpleName();
  	  
  	  /** The Constant DB_NAME. */
  	  private static final String DB_NAME = "tourMap.db";
  	  
  	  /** The Constant DB_VERSION. */
  	  private static final int DB_VERSION = 11;
  	  
  	  /** The m context. */
  	  private Context mContext;
  	  


  /**
   * The default constructor.
   * @param context the context
   */
  public TUeTourDBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
	mContext = context;
    MyLog.v(TAG, "TUeTourDBHelper(%s, %s)", DB_NAME, DB_VERSION);
  }

  /**
   * On create.
   * Create database when it is not exist
   * @param db the db
   * {@inheritDoc}
   */
  @Override
  public void onCreate(SQLiteDatabase db) {
	  MyLog.v(TAG, "onCreate()");
	  
      BufferedReader br = null;
      try {
            // starting the transaction
//            db.beginTransaction();
            String line;
            
            // read file
			br = new BufferedReader(new InputStreamReader(mContext.getAssets().open("tourMap.sql"),
				        "UTF8"));
            while ((line = br.readLine()) != null) {
                    db.execSQL(line);
                  //  Log.w(TAG, line, null);
            }
            // mark the transaction as successful 
            // Transaction not working, cause problem
            // Maybe the onCreate method is already used transaction by default
  //          db.setTransactionSuccessful();
      } catch (Exception e) {
          MyLog.w(TAG, e, "ERROR while copying the database file!");
      } finally {
//    	  	db.endTransaction();
//    	  	db.close();
    	  
              try {
                      if (br != null) {
                              br.close();
                      }
              } catch (Exception e) {
                  MyLog.w(TAG, e, "ERROR while closing the input stream!");
              }
      }
  }

  /**
   * On upgrade.
   * Update database when it is out of version
   * @param db the db
   * @param oldVersion the old version
   * @param newVersion the new version
   * {@inheritDoc}
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      MyLog.v(TAG, "onUpgrade(%s, %s)", oldVersion, newVersion);
      // DO NOTHING   
      onCreate(db);
  }

}